package com.wang.springboot04webrestfulcrud.controller;

import com.wang.springboot04webrestfulcrud.dao.DepartmentDao;
import com.wang.springboot04webrestfulcrud.dao.EmployeeDao;
import com.wang.springboot04webrestfulcrud.entities.Department;
import com.wang.springboot04webrestfulcrud.entities.Employee;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
@Api(value = "/emp",description = "关于用户的操作")
@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;

    @Autowired
    DepartmentDao departmentDao;

//    @ApiOperation("显示所有用户")
    @GetMapping("/emps")
    public String list(Model model){
        Collection<Employee> all = employeeDao.getAll();
        model.addAttribute("emps",all);
        System.out.println("测试");
        System.out.println("测试2");
        return "emp/list";
    }

//    @ApiOperation("到用户添加页面")
    @GetMapping("/emp")
    public String toAddPage(Model model){
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depts",departments);
        return "emp/add";
    }

//    @ApiOperation("添加用户")
//    @ApiImplicitParam(name="user",value="用户实体",required = true,dataType = "user",paramType = "path")
    @PostMapping("/emp")
    public String addEmp(Employee employee){
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    @GetMapping("/emp/{id}")
    @ApiOperation(value="到修改页面",notes = "到用户修改页面，使用携带的id进行回显",response = String.class,tags = "emplyee store")
    @ApiResponse(code = 400, message = "Invalid user supplied")
    @ResponseHeader(name="head1",description="response head conf")
    public String toEditPage(@PathVariable("id") @ApiParam(value="用户id",required=true) Integer id, Model model){
        Employee employee = employeeDao.get(id);
        model.addAttribute("emp",employee);
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depts",departments);
        return "emp/add";
    }

//    @ApiOperation("修改用户")
    @PutMapping("/emp")
    public String updateEmployee (Employee employee){
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/emp/{id}")
    public String deleteEmployee(@PathVariable("id") Integer id){
        System.out.println(id);
        employeeDao.delete(id);
        return "redirect:/emps";
        System.out.println("本地仓库修改");
    }
}
